package com.abss.vote.service.impl;

import com.abss.vote.mapper.ActivityMapper;
import com.abss.vote.mapper.VotesMapper;
import com.abss.vote.mapper.WorkMapper;
import com.abss.vote.model.*;
import com.abss.vote.service.RedisService;
import com.abss.vote.util.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    WorkMapper workMapper;
    @Autowired
    VotesMapper votesMapper;

//-------------------------------------------以下为手动操作--------------------------------------------------------------------------------


    /**
     * 此方法为手动将数据库有效数据写入redis
     */
    @Override
    @Transactional
    public void sqlserverToRedis() {
        //获取全部未删除的活动
        List<Activity> activities = activityMapper.getActivityNoDeled();
        for (Activity ac : activities) {
            //设置总浏览数
            redisTemplate.opsForHash().put(RedisKeyUtils.ACTIVITY_VISIST, ac.getACTIVITY_ID(), ac.getACTIVITY_VISIST());
            //设置报名人数
            redisTemplate.opsForHash().put(RedisKeyUtils.REG_USER, ac.getACTIVITY_ID(), ac.getREG_USER());
            //设置投票人数
            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_USER, ac.getACTIVITY_ID(), ac.getVOTES_USER());
            //设置总点赞数
            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_NUMBER, ac.getACTIVITY_ID(), ac.getVOTES_NUMBER());
            //设置每天限制点赞数
            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_PERDAY, ac.getACTIVITY_ID(), ac.getVOTES_LIMIT_PERDAY());
            //设置作品点赞数
            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, ac.getACTIVITY_ID(), ac.getVOTES_LIMIT_TOSINGLE_PERDAY());
        }
        //获取该活动下未删除的作品
        List<Work> works = workMapper.getAllWork();
        for (Work work : works) {
            String votename = "ACTIVITY_" + work.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VOTES;
            String visitname = "ACTIVITY_" + work.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VISIST;
            //设置作品点赞数
            redisTemplate.opsForHash().put(votename, work.getWORK_ID(), work.getVOTES_NUMBER());
            //设置作品浏览数量
            redisTemplate.opsForHash().put(visitname, work.getWORK_ID(), work.getWORK_VISIST());
        }
    }


//----------------------------------以下为活动----------------------------------------------------------------------------------------


    /**
     * 创建活动
     *
     * @param activityId
     */
    @Override
    @Transactional
    public void addActivity(Integer activityId) {
        //设置总浏览数
        redisTemplate.opsForHash().put(RedisKeyUtils.ACTIVITY_VISIST, activityId, 0);
        //设置报名人数
        redisTemplate.opsForHash().put(RedisKeyUtils.REG_USER, activityId, 0);
        //设置投票人数
        redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_USER, activityId, 0);
        //设置总点赞数
        redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_NUMBER, activityId, 0);
        //设置每天限制点赞数
        redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_PERDAY, activityId, 3);
        //设置作品点赞数
        redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, activityId, 1);
    }

    /**
     * 删除活动(正常)
     *
     * @param activity_id
     * @return
     */
    @Override
    @Transactional
    public int deleteActivity(Integer activity_id) {
//活动
        Activity activity = new Activity();
        activity.setACTIVITY_ID(activity_id);//设置活动id
        //获取总浏览数
        Integer ACTIVITY_VISIST = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.ACTIVITY_VISIST, activity_id);
        activity.setACTIVITY_VISIST(ACTIVITY_VISIST);//设置总浏览数
        //获取报名人数
        Integer REG_USER = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.REG_USER, activity_id);
        activity.setREG_USER(REG_USER);//设置报名人数
        //获取投票人数
        Integer VOTES_USER = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_USER, activity_id);
        activity.setVOTES_USER(VOTES_USER);//设置投票人数
        //获取总点赞数
        Integer VOTES_NUMBER = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_NUMBER, activity_id);
        activity.setVOTES_NUMBER(VOTES_NUMBER);//设置总点赞数
        //删除总浏览数
        redisTemplate.opsForHash().delete(RedisKeyUtils.ACTIVITY_VISIST, activity_id);
        //删除报名人数
        redisTemplate.opsForHash().delete(RedisKeyUtils.REG_USER, activity_id);
        //删除投票人数
        redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_USER, activity_id);
        //删除总点赞数
        redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_NUMBER, activity_id);
        //删除每天限制点赞数
        redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_LIMIT_PERDAY, activity_id);
        //删除作品点赞数
        redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, activity_id);
//作品
        String votename = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VOTES;
        String visitname = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VISIST;
        //点赞数量
        Cursor<Map.Entry<Object, Object>> cursor1 = redisTemplate.opsForHash().scan(votename, ScanOptions.NONE);
        while (cursor1.hasNext()) {
            Map.Entry<Object, Object> map = cursor1.next();
            Integer workId = (Integer) map.getKey();
            Integer WORK_VOTES = (Integer) map.getValue();
            workMapper.setWORK_VOTES(workId, WORK_VOTES);
            //从Redis中删除这条记录
            redisTemplate.opsForHash().delete(votename, workId);
        }
        //浏览数量
        Cursor<Map.Entry<Object, Object>> cursor2 = redisTemplate.opsForHash().scan(visitname, ScanOptions.NONE);
        while (cursor2.hasNext()) {
            Map.Entry<Object, Object> map = cursor2.next();
            Integer workId = (Integer) map.getKey();
            Integer WORK_VISIST = (Integer) map.getValue();
            workMapper.setWORK_VISIST(workId, WORK_VISIST);
            //从Redis中删除这条记录
            redisTemplate.opsForHash().delete(visitname, workId);
        }
        return activityMapper.deUpdateActivity(activity);
    }

    //获取报名人数
    @Override
    public int getREG_USER(int activity_id) {
        return (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.REG_USER, activity_id);
    }

    /**
     * 改版新    将redis中的活动信息写入数据库
     *
     * @param activity_id
     */
    @Override
    public void setActivityDetailFromRedisToSqlServer(int activity_id) {
        Activity activity = new Activity();
        activity.setACTIVITY_ID(activity_id);//设置活动id
        //获取总浏览数
        Integer ACTIVITY_VISIST = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.ACTIVITY_VISIST, activity_id);
        activity.setACTIVITY_VISIST(ACTIVITY_VISIST);//设置总浏览数
        //获取报名人数
        Integer REG_USER = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.REG_USER, activity_id);
        activity.setREG_USER(REG_USER);//设置报名人数
        //获取投票人数
        Integer VOTES_USER = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_USER, activity_id);
        activity.setVOTES_USER(VOTES_USER);//设置投票人数
        //获取总点赞数
        Integer VOTES_NUMBER = (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_NUMBER, activity_id);
        activity.setVOTES_NUMBER(VOTES_NUMBER);//设置总点赞数
        activityMapper.deUpdateActivity(activity);
    }


//-----------------------------------------以下为作品-----------------------------------------------------------------------------------

    /**
     * 改版新    将redis中的作品信息写入数据库
     *
     * @param activity_id
     */
    @Override
    public void setWorkDetailFromRedisToSqlServer(int activity_id) {
        String votename = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VOTES;
        String visitname = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VISIST;
        //点赞数量
        Cursor<Map.Entry<Object, Object>> cursor1 = redisTemplate.opsForHash().scan(votename, ScanOptions.NONE);
        while (cursor1.hasNext()) {
            Map.Entry<Object, Object> map = cursor1.next();
            Integer workId = (Integer) map.getKey();
            Integer WORK_VOTES = (Integer) map.getValue();
            workMapper.setWORK_VOTES(workId, WORK_VOTES);
        }
        //浏览数量
        Cursor<Map.Entry<Object, Object>> cursor2 = redisTemplate.opsForHash().scan(visitname, ScanOptions.NONE);
        while (cursor2.hasNext()) {
            Map.Entry<Object, Object> map = cursor2.next();
            Integer workId = (Integer) map.getKey();
            Integer WORK_VISIST = (Integer) map.getValue();
            workMapper.setWORK_VISIST(workId, WORK_VISIST);
        }
    }


//------------------------------------------以下为投票----------------------------------------------------------------------------------

    //    //获取每天限制点赞数
    @Override
    public int getVOTES_LIMIT_PERDAY(int activity_id) {
        return (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_LIMIT_PERDAY, activity_id);
    }

    //获取作品点赞数
    @Override
    public int getVOTES_LIMIT_TOSINGLE_PERDAY(int activity_id) {
        return (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, activity_id);
    }

    //获取该用户的投票信息
    @Override
    public UserVoteInfo getUSERVOTESINFO(int ACTIVITY_ID, String USER_ID, int WORK_ID) {
        String votename = "ACTIVITY_" + ACTIVITY_ID + "_" + RedisKeyUtils.VOTES;
        String userVoteInfo = (String) redisTemplate.opsForHash().get(votename, USER_ID);
        UserVoteInfo userVoteInfo1 = new UserVoteInfo();
        if (userVoteInfo != null) {
            String[] val = userVoteInfo.split(":::");
            int allnumber = 0;
            int worknumber = 0;
            for (int i = 0; i < val.length; i++) {
                allnumber += 1;
                String[] workn = val[i].split("::");
                Integer workid = Integer.valueOf(workn[0]);
                if (WORK_ID == workid) {
                    worknumber += 1;
                }
            }
            userVoteInfo1.setAllVoteNumber(allnumber);
            userVoteInfo1.setWorkVoteNumber(worknumber);
        } else {
            userVoteInfo1.setAllVoteNumber(0);
            userVoteInfo1.setWorkVoteNumber(0);
        }
        return userVoteInfo1;
    }


    //添加活动总投票数
    @Override
    public void addVote(int ACTIVITY_ID, String USER_ID, int WORK_ID) {
        //设置活动投票人数加一
        //首先判断该用户在此活动中是否参与
        String votename1 = "ACTIVITY_" + ACTIVITY_ID + "_" + RedisKeyUtils.USER_VOTES_INFO;
        String userInfo = (String) redisTemplate.opsForHash().get(votename1, USER_ID);
        if (userInfo == null) {
            redisTemplate.opsForHash().increment(RedisKeyUtils.VOTES_USER, ACTIVITY_ID, 1);
            //用户投票信息写入redis
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            redisTemplate.opsForHash().put(votename1, USER_ID, sdf.format(new Date()));
        }

        //将用户点赞传过来的信息写入redis
        String votename2 = "ACTIVITY_" + ACTIVITY_ID + "_" + RedisKeyUtils.VOTES;
        String userVoteInfo = (String) redisTemplate.opsForHash().get(votename2, USER_ID);
        if (userVoteInfo == null) {
            userVoteInfo = "1";
        }
        String value = RedisKeyUtils.setVOTESVALUE(userVoteInfo, WORK_ID);
        redisTemplate.opsForHash().put(votename2, USER_ID, value);

        //添加活动总投票数
        redisTemplate.opsForHash().increment(RedisKeyUtils.VOTES_NUMBER, ACTIVITY_ID, 1);

        //作品点赞数加一
        String votename4 = "ACTIVITY_" + ACTIVITY_ID + "_" + RedisKeyUtils.WORK_VOTES;
        redisTemplate.opsForHash().increment(votename4, WORK_ID, 1);
    }

    //添加浏览数
    @Override
    public void setVisist(int activity_id) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.ACTIVITY_VISIST, activity_id, 1);
    }

    //作品浏览数加一
    @Override
    public void setWorkVisist(int activity_id, Integer work_id) {
        String votename = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VISIST;
        redisTemplate.opsForHash().increment(votename, work_id, 1);
    }

    //获取浏览数
    @Override
    public int getWorkVisist(int activity_id, Integer work_id) {
        String votename = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VISIST;
        return (Integer) redisTemplate.opsForHash().get(votename, work_id);
    }

    @Override
    public Integer getWOrkVotesNumber(int activity_id, Integer work_id) {
        String votename = "ACTIVITY_" + activity_id + "_" + RedisKeyUtils.WORK_VOTES;
        return (Integer) redisTemplate.opsForHash().get(votename, work_id);
    }

    //获取投票人数
    @Override
    public int getVotesUser(int activity_id) {
        return (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_USER, activity_id);
    }

    @Override
    public int getVotesNumber(int activity_id) {
        return (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.VOTES_NUMBER, activity_id);
    }


    //获取浏览数
    @Override
    public int getVisist(int activity_id) {
        return (Integer) redisTemplate.opsForHash().get(RedisKeyUtils.ACTIVITY_VISIST, activity_id);
    }

    //减少报名人数
    @Override
    public void delREG_USER(int activity_id) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.REG_USER, activity_id, -1);
    }

    //添加报名人数
    @Override
    public void setREG_USER(int activity_id) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.REG_USER, activity_id, 1);
    }

    //新增作品
    @Override
    public void addWork(Work work) {
        String votename = "ACTIVITY_" + work.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VOTES;
        String visitname = "ACTIVITY_" + work.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VISIST;
        //设置作品点赞数
        redisTemplate.opsForHash().put(votename, work.getWORK_ID(), 0);
        //设置作品浏览数量
        redisTemplate.opsForHash().put(visitname, work.getWORK_ID(), 0);
    }


    //设置作品点暂数
    @Override
    public void setVotesLinitPerday(int votes_limit_perday, int activity_id) {
        redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_PERDAY, activity_id, votes_limit_perday);
    }

    //设置每天限制点暂数
    @Override
    public void setVotesLimitTosinglePerday(int votes_limit_tosingle_perday, int activity_id) {
        redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, activity_id, votes_limit_tosingle_perday);
    }


    //将redis中点赞信息数据写入数据库并清除redis中点赞信息数据
    @Override
    public void addUSERVOTESINFO() {
        //获取全部未删除的活动
        List<Activity> activities = activityMapper.getActivityNoDeled();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Activity ac : activities) {
            String votename = "ACTIVITY_" + ac.getACTIVITY_ID() + "_" + RedisKeyUtils.VOTES;
            Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(votename, ScanOptions.NONE);
            while (cursor.hasNext()) {
                Votes votes = new Votes();
                votes.setACTIVITY_ID(ac.getACTIVITY_ID());
                try {
                    Map.Entry<Object, Object> entry = cursor.next();
                    String key = (String) entry.getKey();
                    votes.setUSER_ID(key);

                    String DTIME = (String) entry.getValue();
                    String[] value = DTIME.split(":::");
                    for (int i = 0; i < value.length; i++) {
                        String va = value[i];
                        Integer workId = Integer.valueOf(va.split("::")[0]);
                        String time = va.split("::")[1];
                        Date date = sdf.parse(time);
                        votes.setWORK_ID(workId);
                        votes.setDTIME(date);
                        votesMapper.addVotes(votes);
                    }
                    //从Redis中删除这条记录
                    redisTemplate.opsForHash().delete(votename, key);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void addUserVisitAndVoteInfoFromRedisToServer() {
        //获取全部未删除的活动
        List<Activity> activities = activityMapper.getActivityNoDeled();
        for (Activity ac : activities) {
            Cursor<Map.Entry<Object, Object>> cursor1 = redisTemplate.opsForHash().scan("ACTIVITY_" + ac.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VOTES, ScanOptions.NONE);
            while (cursor1.hasNext()) {
                Map.Entry<Object, Object> map = cursor1.next();
                Integer workId = (Integer) map.getKey();
                Integer WORK_VOTES = (Integer) map.getValue();
                workMapper.setWORK_VOTES(workId, WORK_VOTES);
                //从Redis中删除这条记录
               // redisTemplate.opsForHash().delete("ACTIVITY_" + ac.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VOTES, workId);

            }
            //浏览数量
            Cursor<Map.Entry<Object, Object>> cursor2 = redisTemplate.opsForHash().scan("ACTIVITY_" + ac.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VISIST, ScanOptions.NONE);
            while (cursor2.hasNext()) {
                Map.Entry<Object, Object> map = cursor2.next();
                Integer workId = (Integer) map.getKey();
                Integer WORK_VISIST = (Integer) map.getValue();
                workMapper.setWORK_VISIST(workId, WORK_VISIST);
                //从Redis中删除这条记录
                //redisTemplate.opsForHash().delete("ACTIVITY_" + ac.getACTIVITY_ID() + "_" + RedisKeyUtils.WORK_VISIST, workId);
            }


        }
    }


    //    //查询该用户在该活动中投过多少票
//    @Override
//    public int getUSERVOTESINFOs(String activityId,String userId) {
//        int res = 0;
//        String key = RedisKeyUtils.setVOTESkey(activityId,userId);
//        Cursor<Map.Entry<Object, Object>> cursor=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES, ScanOptions.NONE);
//        while (cursor.hasNext()){
//            Map.Entry<Object, Object> entry = cursor.next();
//            String keys = (String) entry.getKey();
//            String[] split = keys.split("::");
//            String ACTIVITY_ID = split[0];
//            String USER_ID = split[2];
//            String vote = RedisKeyUtils.setVOTESkey(ACTIVITY_ID,USER_ID);
//            if(key.equals(vote)){//有值修改
//                String DTIME = (String)entry.getValue();
//                String[] value = DTIME.split("::");
//                res+=value.length;
//            }
//        }
//        return res;
//    }
//
//    //查询该用户对该作品投过几次票
//    @Override
//    public int getUSERVOTESINFOss(String workId, String user_id) {
//        int res = 0;
//        String key = RedisKeyUtils.setVOTESkey(workId,user_id);
//        Cursor<Map.Entry<Object, Object>> cursor=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES, ScanOptions.NONE);
//        while (cursor.hasNext()){
//            Map.Entry<Object, Object> entry = cursor.next();
//            String keys = (String) entry.getKey();
//            String[] split = keys.split("::");
//            String WORK_ID = split[1];
//            String USER_ID = split[2];
//            String vote = RedisKeyUtils.setVOTESkey(WORK_ID,USER_ID);
//            if(key.equals(vote)){//有值修改
//                String DTIME = (String)entry.getValue();
//                String[] value = DTIME.split("::");
//                res+=value.length;
//            }
//        }
//        return res;
//    }


//    /**
//     * 将redis中的活动信息写入数据库中并清除
//     */
//    @Override
//    @Transactional
//    public void addActivity() {
//        //浏览数量
//        Cursor<Map.Entry<Object, Object>> cursor1=redisTemplate.opsForHash().scan(RedisKeyUtils.ACTIVITY_VISIST, ScanOptions.NONE);
//        while (cursor1.hasNext()){
//            Map.Entry<Object, Object> map = cursor1.next();
//            String activityId = (String) map.getKey();
//            Integer ACTIVITY_VISIST = (Integer)map.getValue();
//            activityMapper.setACTIVITY_VISIST(Integer.parseInt(activityId),ACTIVITY_VISIST);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.ACTIVITY_VISIST, activityId);
//        }
//        //报名人数
//        Cursor<Map.Entry<Object, Object>> cursor2=redisTemplate.opsForHash().scan(RedisKeyUtils.REG_USER, ScanOptions.NONE);
//        while (cursor2.hasNext()){
//            Map.Entry<Object, Object> map = cursor2.next();
//            String activityId = (String) map.getKey();
//            Integer REG_USER = (Integer)map.getValue();
//            activityMapper.setREG_USER(Integer.parseInt(activityId),REG_USER);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.REG_USER, activityId);
//        }
//        //投票人数
//        Cursor<Map.Entry<Object, Object>> cursor3=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES_USER, ScanOptions.NONE);
//        while (cursor3.hasNext()){
//            Map.Entry<Object, Object> map = cursor3.next();
//            String activityId = (String)map.getKey();
//            Integer VOTES_USER = (Integer)map.getValue();
//            activityMapper.setVOTES_USER(activityId,VOTES_USER);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_USER, activityId);
//        }
//        //投票数
//        Cursor<Map.Entry<Object, Object>> cursor4=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES_NUMBER, ScanOptions.NONE);
//        while (cursor4.hasNext()){
//            Map.Entry<Object, Object> map = cursor4.next();
//            String activityId = (String) map.getKey();
//            Integer VOTES_NUMBER = (Integer)map.getValue();
//            activityMapper.setVOTES_NUMBER(Integer.parseInt(activityId),VOTES_NUMBER);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_NUMBER, activityId);
//        }
//        //删除redis中每天限制投票数
//        Cursor<Map.Entry<Object, Object>> cursor5=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES_LIMIT_PERDAY, ScanOptions.NONE);
//        while (cursor5.hasNext()){
//            Map.Entry<Object, Object> map = cursor5.next();
//            String activityId = (String) map.getKey();
//            Integer VOTES_LIMIT_PERDAY = (Integer)map.getValue();
//            activityMapper.setVOTES_LIMIT_PERDAY(Integer.parseInt(activityId),VOTES_LIMIT_PERDAY);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_LIMIT_PERDAY, activityId);
//        }
//        //删除redis中作品投票数
//        Cursor<Map.Entry<Object, Object>> cursor6=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, ScanOptions.NONE);
//        while (cursor6.hasNext()){
//            Map.Entry<Object, Object> map = cursor6.next();
//            String activityId = (String)map.getKey();
//            Integer VOTES_LIMIT_TOSINGLE_PERDAY = (Integer)map.getValue();
//            activityMapper.setVOTES_LIMIT_TOSINGLE_PERDAY(Integer.parseInt(activityId),VOTES_LIMIT_TOSINGLE_PERDAY);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, activityId);
//        }
//    }
//
//    /**
//     * 将数据库中能查看的活动信息添加到redis中
//     */
//    @Override
//    public void setActivityNoDeled(){
//        List<Activity> list = activityMapper.getActivityNoDeled();
//        for(Activity ac:list){
//            String activityId =String.valueOf(ac.getACTIVITY_ID());
//            //设置总浏览数
//            redisTemplate.opsForHash().put(RedisKeyUtils.ACTIVITY_VISIST, activityId, ac.getACTIVITY_VISIST());
//            //设置报名人数
//            redisTemplate.opsForHash().put(RedisKeyUtils.REG_USER, activityId, ac.getREG_USER());
//            //设置投票人数
//            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_USER, activityId, ac.getVOTES_USER());
//            //设置总点赞数
//            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_NUMBER, activityId, ac.getVOTES_NUMBER());
//            //设置每天限制点赞数
//            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_PERDAY, activityId, ac.getVOTES_LIMIT_PERDAY());
//            //设置作品点赞数
//            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_LIMIT_TOSINGLE_PERDAY, activityId, ac.getVOTES_LIMIT_TOSINGLE_PERDAY());
//        }
//    }
//

//

//

//

//

//

//

//

//

//

//

//

//

//

//
//    //将redis中作品数据写入数据库并清除redis中作品数据
//    @Override
//    public void addWork() {
//        //点赞数量
//        Cursor<Map.Entry<Object, Object>> cursor1=redisTemplate.opsForHash().scan(RedisKeyUtils.WORK_VOTES, ScanOptions.NONE);
//        while (cursor1.hasNext()){
//            Map.Entry<Object, Object> map = cursor1.next();
//            Integer workId = (Integer)map.getKey();
//            Integer WORK_VOTES = (Integer)map.getValue();
//            workMapper.setWORK_VOTES(workId,WORK_VOTES);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.WORK_VOTES, workId);
//        }
//        //浏览数量
//        Cursor<Map.Entry<Object, Object>> cursor2=redisTemplate.opsForHash().scan(RedisKeyUtils.WORK_VISIST, ScanOptions.NONE);
//        while (cursor2.hasNext()){
//            Map.Entry<Object, Object> map = cursor2.next();
//            Integer workId = (Integer)map.getKey();
//            Integer WORK_VISIST = (Integer)map.getValue();
//            workMapper.setWORK_VISIST(workId,WORK_VISIST);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.WORK_VISIST, workId);
//        }
//    }
////
//    //将数据库中作品数据写入redis
//    @Override
//    public void setWork() {
//        List<Work> list = workMapper.getAllWork();
//        for(Work work:list){
//            int workId= work.getWORK_ID();
//            //设置作品点赞数
//            redisTemplate.opsForHash().put(RedisKeyUtils.WORK_VOTES, workId, work.getVOTES_NUMBER());
//            //设置作品浏览数量
//            redisTemplate.opsForHash().put(RedisKeyUtils.WORK_VISIST, workId, work.getWORK_VISIST());
//        }
//    }
//
//    //数据库中点赞信息写入redis并设置点赞人数
//    @Override
//    public void setUserVotesInfo() {
//        Map<String,Integer> map=new HashedMap();
//        List<UserVotesInfo> votes = votesMapper.getVotes();
//        for(UserVotesInfo vo:votes) {
//            String ActivityId = String.valueOf(vo.getACTIVITY_ID());
//            String key = RedisKeyUtils.setUSER_VOTES_INFO(ActivityId, vo.getUSER_ID());
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String time = sdf.format(vo.getLAST_UPDATE_TIME());
//            redisTemplate.opsForHash().put(RedisKeyUtils.USER_VOTES_INFO, key, time);
//            if(map.get(ActivityId) == null){
//                map.put(ActivityId,1);
//            }else{
//                map.put(ActivityId,map.get(ActivityId)+1);
//            }
//        }
//        for(String key:map.keySet()) {//设置点赞人数
//            redisTemplate.opsForHash().put(RedisKeyUtils.VOTES_USER, key, map.get(key));
//        }
//
//    }
//

//
//
//    //用户投票信息写入redis
//    @Override
//    public void inSertUSERVOTESINFO(String activityId, String userId) {
//        String key = RedisKeyUtils.setUSER_VOTES_INFO(activityId,userId);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        redisTemplate.opsForHash().put(RedisKeyUtils.USER_VOTES_INFO, key, sdf.format(new Date()));
//    }
//

//
//    //查询redis中是否有该用户的投票信息
//    @Override
//    public int getUSERVOTESINFO(String userId) {
//        int res = 0;
//        Cursor<Map.Entry<Object, Object>> cursor=redisTemplate.opsForHash().scan(RedisKeyUtils.VOTES, ScanOptions.NONE);
//        while (cursor.hasNext()){
//            Map.Entry<Object, Object> entry = cursor.next();
//            String keys = (String) entry.getKey();
//            String[] split = keys.split("::");
//            String USER_ID = split[2];
//            if(USER_ID.equals(userId)){//有值修改
//                String DTIME = (String)entry.getValue();
//                String[] value = DTIME.split("::");
//                res+=value.length;
//            }
//        }
//        return res;
//    }
//
//
//    //获取作品投票数量
//    @Override
//    public int getWOrkVotesNumber(int workId) {
//        int res=0;
//        //点赞数量
//        Cursor<Map.Entry<Object, Object>> cursor1=redisTemplate.opsForHash().scan(RedisKeyUtils.WORK_VOTES, ScanOptions.NONE);
//        while (cursor1.hasNext()){
//            Map.Entry<Object, Object> map = cursor1.next();
//            Integer Id = (Integer)map.getKey();
//            Integer WORK_VOTES = (Integer)map.getValue();
//            if(workId==Id){
//                res=WORK_VOTES;
//            }
//        }
//        return res;
//    }
}
